package com.qeegoo.b2bautozityp.moudle.good.adapter;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.entity.Category;
import com.qeegoo.b2bautozityp.moudle.good.entity.Level0Item;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsCategoryBean;
import com.qeegoo.b2bautozityp.moudle.good.view.category.GoodsCategoryActivity;
import com.qeegoo.b2bautozityp.moudle.good.view.category.GoodsCategoryFragment;

import java.util.ArrayList;
import java.util.List;

import base.lib.util.ActivityManager;

/**
 * 王少岩 在 2017/9/20 创建了它
 */

public class ExpandableItemAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_LEVEL_1 = 1;
    private RequestApi mRequestApi;

    public ExpandableItemAdapter(List<MultiItemEntity> data, RequestApi requestApi) {
        super(data);
        mRequestApi = requestApi;
        addItemType(TYPE_LEVEL_0, R.layout.adapter_goods_category_0);
        addItemType(TYPE_LEVEL_1, R.layout.adapter_goods_category_1);
    }

    @Override
    protected void convert(BaseViewHolder holder, MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_LEVEL_0:
                final Level0Item level0Item = (Level0Item) item;
                holder.setText(R.id.tv_title, level0Item.getGoodsCategory().name);
                holder.itemView.setOnClickListener(v -> {
                    int pos = holder.getAdapterPosition();
                    if (level0Item.isExpanded()) {
                        collapse(pos);
                    } else {
                        if (level0Item.getSubItems() == null)
                            getCategorys(level0Item, pos);
                        else
                            expand(pos);
                    }
                });
                break;
            case TYPE_LEVEL_1:
                final Category category = (Category) item;
                holder.setText(R.id.tv_subtitle, category.getGoodsCategory().name);
                holder.itemView.setOnClickListener(v -> {
//                    holder.setVisible(R.id.iv_selected, true);
//                    holder.setTextColor(R.id.tv_subtitle, ActivityManager.getActivity().getResources().getColor(R.color.blue));
                    ((GoodsCategoryActivity) ActivityManager.getActivity()).openGoodsCategoryFrag(GoodsCategoryFragment.newsInstance(category.getGoodsCategory().name, category.getGoodsCategory().id));
                });
                break;
        }
    }

    public void getCategorys(Level0Item level0Item, int pos) {
        mRequestApi.listProductByLevel(HttpParams.goodsCategory(level0Item.getGoodsCategory().id))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<GoodsCategoryBean>>() {
                    @Override
                    public void onNext(ArrayList<GoodsCategoryBean> goodsCategorys) {
                        for (GoodsCategoryBean bean : goodsCategorys) {
                            level0Item.addSubItem(new Category(bean));
                        }
                        expand(pos);
                    }
                });
    }
}
